﻿namespace Command_Pattern
{
    public sealed class Inventory
    {
        private static List<Food> foods = new List<Food>()
                        {
                            new Food("羊肉串",10),
                            new Food("鸡翅",10),
                            new Food("热狗",10)
                        };
        private static readonly object syncRoot = new object();
        private Inventory() { }
       
        public static bool ReduceInventory(Food food)
        {
            var oldFood = foods.FirstOrDefault(x=>x.Name == food.Name);
            if (!oldFood.Reduce(food.Count))
            {
                return false;
            }
            foods = foods.Select(x=>x.Name == food.Name?oldFood:x).ToList();
            return true;
        }

        public static void IncreaseInventory(Food food)
        {
            var oldFood = foods.FirstOrDefault(x => x.Name == food.Name);
            oldFood.Increase(food.Count);
            foods = foods.Select(x => x.Name == food.Name ? oldFood : x).ToList();
        }

        public static int RemainingInventory(string name)
        {
            var food = foods.FirstOrDefault(x => x.Name == name);
            return food == null ? 0 :food.Count;   
        }

    }

    public class Food()
    {
        public string? Name { get; set; }
        public int Count { get; set; }
        public Food(string name,int count):this()
        {
            Name = name;
            Count = count;
        }
        public bool Reduce(int count)
        {
            if(count >Count)
            {
                return false;
            }
            Count -= count;
            return true;
        }
        public void Increase(int count)
        {
            Count+=count;
        }
    }
}
